package com.kkd.kkdapp.view;

import java.util.HashMap;
import java.util.Map;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.utils.SystemUtils;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class MyRelativeLayout extends RelativeLayout implements OnTouchListener {

	private Context context;
	private Map<Integer, RelativeLayout> map;
	private float downX = 0;
	private float downY = 0;
	private float moveX = 0;
	private float moveY = 0;
	private float imgX = 0;
	private float imgY = 0;
	private float upX = 0;
	private float upY = 0;
	private RelativeLayout ImgNo1;
	private RelativeLayout imgLayout;
	private int position = -1;
	private OnItemClick onitemclick;
	private Map<Integer, RelativeLayout> liveMap = new HashMap<Integer, RelativeLayout>();

	public MyRelativeLayout(Context context) {
		super(context);
		this.context = context;
	}

	public MyRelativeLayout(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.context = context;
	}

	public MyRelativeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		removeAllViews();
		View view = LayoutInflater.from(context).inflate(
				R.layout.characteristicdialog, null);
		imgLayout = (RelativeLayout) view.findViewById(R.id.layout);
		addView(view);
	}

	// 更新UI
	public void updateUI() {

		int margin = 0;

		imgLayout.removeAllViews();
		Log.e("map", map.size() + "");
		for (int individual = map.size() - 1; individual >= 0; individual--) {
			RelativeLayout img = map.get(individual);
			LayoutParams params = new RelativeLayout.LayoutParams(
					SystemUtils.dip2px(context, 224), SystemUtils.dip2px(context,
							275));
			params.topMargin = SystemUtils.dip2px(context, margin);
			img.setLayoutParams(params);
			imgLayout.addView(img);
			margin = margin + 5;
		}
		if (position >= map.size() - 1) {
			position = 0;
		} else {
			position = position + 1;
		}

	}

	public void setDataSource(Map<Integer, RelativeLayout> map) {
		this.map = map;
		for (int ag = 0; ag < map.size(); ag++) {
			liveMap.put(ag, map.get(ag));
		}
	}

	public void setTouchListener() {
		imgLayout.setOnTouchListener(this);
	}

	@SuppressLint("NewApi")
	public void translatingAnimation(View view, float fromX, float toX,
			float fromY, float toY) {
		ValueAnimator animationX = ObjectAnimator.ofFloat(view, "translationX",
				SystemUtils.px2dip(context, fromX), 0);
		ValueAnimator animationY = ObjectAnimator.ofFloat(view, "translationY",
				SystemUtils.px2dip(context, fromY), 0);
		AnimatorSet animSet = new AnimatorSet();
		animSet.play(animationX).with(animationY);
		animSet.setDuration(500);
		animSet.start();
	}

	// 重塑数据
	public void hierarchicalOperation() {
		ImgNo1 = map.get(0);
		for (int j = 0; j < map.size(); j++) {
			if (j == map.size() - 1)
				map.put(map.size() - 1, ImgNo1);
			else
				map.put(j, map.get(j + 1));
		}
		updateUI();
	}

	private long dowMillis;
	private Long upMillis;

	@SuppressLint("NewApi")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			imgX = map.get(0).getLeft();
			imgY = map.get(0).getTop();
			downX = event.getX();
			downY = event.getY();
			map.get(0).setX(imgX);
			map.get(0).setY(imgY);
			Log.e("TAG", "down->" + downX);
			dowMillis = System.currentTimeMillis() / 1000;
			if (downX >= map.get(0).getLeft() && downX <= map.get(0).getRight()
					&& downY >= map.get(0).getTop()
					&& downY <= map.get(0).getBottom()) {
				return true;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			moveX = event.getX();
			moveY = event.getY();
			Log.e("TAG", "move->" + moveX);
			float imgX2 = 0;
			float imgY2 = 0;
			imgX2 = imgX + (moveX - downX);
			imgY2 = imgY + (moveY - downY);
			if (downX >= map.get(0).getLeft() && downX <= map.get(0).getRight()
					&& downY >= map.get(0).getTop()
					&& downY <= map.get(0).getBottom()) {
				Log.e("TAG", "您通过了验证~~~~~");
				map.get(0).setX(imgX2);
				map.get(0).setY(imgY2);
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:
			upX = event.getX();
			upY = event.getY();
			upMillis = System.currentTimeMillis() / 1000;
			if (downX >= map.get(0).getLeft() && downX <= map.get(0).getRight()
					&& downY >= map.get(0).getTop()
					&& downY <= map.get(0).getBottom()) { // 点击是否在图片范围内
				inACertainRange();
				return true;
			}
			break;
		}
		return false;
	}

	public void inACertainRange() {

		Log.e("TAG", "downX" + downX);
		Log.e("TAG", "moveX" + moveX);

		if (moveX == 0) { // moveX=0时认为是点击事件

			ClickEvent();

		} else {

			if (Math.abs(moveX - downX) > SystemUtils.dip2px(context, 100)) { // Math.abs(moveX
																				// -
																				// downX)
				// > 600时认为是滑动事件
				slide();

			} else {

				translatingAnimation(map.get(0), upX - downX, imgX,
						upY - downY, imgY);

				if (moveX - downX < SystemUtils.dip2px(context, 10)
						&& moveY - downY < SystemUtils.dip2px(context, 10)) {

					// moveX - downX < SystemUtil.dip2px(context, 10)
					// && moveY - downY < SystemUtil.dip2px(context,
					// 10)时认为是点击事件

					ClickEvent();

				}

			}
		}
	}

	// 是否滑动
	public void slide() {
		hierarchicalOperation();
		translatingAnimation(map.get(map.size() - 1), upX - downX, imgX, upY
				- downY, imgY);
		moveX = 0;
		downX = 0;
	}

	/**
	 * 点击事件
	 */
	public void ClickEvent() {
		if (upMillis - dowMillis <= 1) {
			if (onitemclick != null) {
				onitemclick.getPosition(position);
			}
		}
	}

	public void setOnItemClick(OnItemClick onitemclick) {
		this.onitemclick = onitemclick;
	}

	// 回调接口
	public interface OnItemClick {

		public void getPosition(int position);

	}

	public void setCurrentItem(int currentItem) {

		position = currentItem - 2;
		int selectLocation = currentItem - 1;
		int afterAfew = map.size() - currentItem;
		int location = 0;
		int thereAreSeveral = -1;
		for (int item = 0; item < map.size(); item++) {
			if (item == 0) {

				map.put(item, liveMap.get(selectLocation));

			} else if (item > 0) {

				if (location <= afterAfew) {
					map.put(item, liveMap.get(selectLocation + location));

				} else {
					thereAreSeveral = thereAreSeveral + 1;
					map.put(item, liveMap.get(thereAreSeveral));
				}

			}
			location++;
		}

		updateUI();
	}
}
